import {Component, OnInit} from '@angular/core';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {QuestionListService} from './list.service';
import {SearchParams} from '@core/interfaces/table.interface';
import {GridComponent} from '@core/grid.component';
import {QuestionEditComponent} from 'app/routes/question/list/questionEdit/edit.component';
import {QuestionModel, QuestionListModel} from './model/questionModel';

@Component({
  selector: 'app-question-list',
  templateUrl: 'list.component.html',
  styleUrls: ['./list.component.less'],
  providers: [QuestionListService]
})
export class QuestionListComponent extends GridComponent implements OnInit {

  /**
   * 列表搜索数据模型
   */
  searchParams: SearchParams = {
    params: {
      levelList: [],
      kindList: [],
      typeList: []
    },
    values: {
      kind: '',
      level: '',
      startTime: '',
      endTime: '',
      description: ''
    }
  };
  constructor(private questionListService: QuestionListService, private message: NzMessageService, private modalService: NzModalService) {
    super(questionListService);
    this.questionListService.getKindList().then((response: any[]) => {
      this.searchParams.params.kindList = response;
    });
    this.searchParams.params.levelList = this.questionListService.getLevelList();
    this.searchParams.params.typeList = this.questionListService.getTypeList();

    /**
     * 列表操作按钮
     */
    this.setGridView('operations', [
      {
        text: '修改',
        key: 'qid',
        event: (id) => {
          this.findOneAndUpdate(id);
        }
      },
      {
        text: '删除',
        isConfirm: true,
        title: '确定要删除这个题目吗?',
        key: 'qid',
        event: {
          ok: (id) => {
            this.delete(id);
          },
          cancel: () => {
            this.message.info('click cancel');
          }
        }
      }
    ]);
  }

  ngOnInit() {
    this.getData();
  }

  /**
   * 获取列表数据
   * @param {number} event
   */
  getData = (event?: number) => {
    this.setLoading(true);
    this.current = event ? event : this.current;
    this.questionListService.queryList(this.current, this.pageSize, this.searchParams.values)
      .then((result: QuestionListModel) => {
        result.content.forEach(function (value) {
          value.detailOption = '';
          value.options.forEach(childVal => {
            value.detailOption += childVal.key + ':' + childVal.value + ',';
          });
          value.detailOption = value.detailOption.substring(0, value.detailOption.length-1);
        });
        this.setTableData(result.content);
        this.setTotal(result.total);
        this.setLoading(false);
      });
  }


  /**
   * 新增或修改单条数据
   * @param {string} id
   */
  createOrUpdate(questionModel?: QuestionModel) {
    const self = this;
    const subscription = this.modalService.open({
      title: questionModel ? 'update' : 'create',
      content: QuestionEditComponent,
      footer: false,
      onOk() {
        self.getData();
      },
      onCancel() {
      },
      componentParams: {
        questionModel: questionModel,
        kindList: this.searchParams.params.kindList,
        levelList: this.searchParams.params.levelList,
        typeList: this.searchParams.params.typeList
      }
    });
    subscription.subscribe(result => {
      if (result === 'ok') {
        this.getData();
      }
    });
  }

  /**
   * 删除单条
   * @param {string} id
   */
  delete(id: string) {
    console.log(id);
    this.questionListService.remove(id).then((response: any) => {
      this.getData();
    });
  }

  submitForm(formData?: any) {
    console.log(formData)
  }

  /**
   * 清除搜索框数据
   */
  clearSearchParams() {
    for (const key in this.searchParams.values) {
      this.searchParams.values[key] = '';
    }
  }

  _startValueChange = () => {
    const {startTime, endTime} = this.searchParams.values;
    if (startTime > endTime) {
      this.searchParams.values.endTime = null;
    }
  }

  _endValueChange = () => {
    const {startTime, endTime} = this.searchParams.values;
    if (startTime > endTime) {
      this.searchParams.values.startTime = null;
    }
  }

  findOneAndUpdate(id: any) {
    this.questionListService.queryById(id).then((response: QuestionModel) => {
      console.log(response);
      this.createOrUpdate(response);
    })
  }
}
